package com.nf.health.app.activity.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.widget.CircularImage;
import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.inquiry.docdetail.DocCommentDiscussActivity;
import com.nf.health.app.models.DoctorCommnetVo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageUtils;
import com.nf.health.app.utils.TextUtilTools;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by changpeng on 2017/4/8 0008.
 */

public class DoctorDetailsAdapter extends RecyclerView.Adapter  {
    private static final int TYPE_HEAD = 0;
    private static final int TYPE_CONTENT = 1;

    private List<DoctorCommnetVo> comments;
    private Context mContext;
    private String goodAt,content,doctorId,totalCount;
    public DoctorDetailsAdapter(Context context, String goodAt,
                                String content, String doctorId, String totalCount,
                                List<DoctorCommnetVo> comments) {
        this.mContext = context;
        this.comments = comments;
        this.goodAt = goodAt;
        this.content = content;
        this.totalCount = totalCount;
        this.doctorId = doctorId;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_doctor_info_head, parent, false);
                return new HeadHolder(view);
            case TYPE_CONTENT:
                view = LayoutInflater.from(mContext).inflate(R.layout.view_comment_list, parent, false);
                return new ContentHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadHolder) {
            HeadHolder headHolder = (HeadHolder) holder;
        }

        if (holder instanceof ContentHolder) {
            DoctorCommnetVo bean = comments.get(position - 1);
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.reFreshMyTvText(bean);
        }

    }

    @Override
    public int getItemCount() {
        if(comments == null){
            return 1;
        }
        return comments.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD;
        }
        return TYPE_CONTENT;
//        return super.getItemViewType(position);
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_base_info)
        TextView tvBaseInfo;
        @Bind(R.id.tv_all_eva)
        TextView tvAllEva;
        @Bind(R.id.tv_comment)
        TextView tvComment; //用户评论
        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            initView();
            initClick();
        }

        private void initView() {
            TextUtilTools.setTextFromHtml(tvBaseInfo, "<b>擅长:</b>" + goodAt + "<br><br>" + "<b>简介:</b>" + content);
            tvComment.setText("用户评价("+totalCount+"人)");
        }

        private void initClick() {
            tvAllEva.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //需要传doctorid
                    Intent intent = new Intent(mContext,DocCommentDiscussActivity.class);
                    intent.putExtra(Constants.DOCTOR_ID,doctorId);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_img_discuss)
        CircularImage iv_img_discuss;//医生的头像
        @Bind(R.id.tv_discuss_doc_name)//医生姓名
                TextView tv_discuss_doc_name;
        @Bind(R.id.tv_is_satisfied)//满意程度
                TextView tv_is_satisfied;
        @Bind(R.id.mygv_status_satify)//存放GidView的满意程度
                MyFlowLayout mFlowLayout;
        @Bind(R.id.tv_discuss_doc_time)//评论的时间
                TextView tv_discuss_doc_time;
        @Bind(R.id.tv_word_discuss)//医生评论
                TextView tv_word_discuss;
        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        /**
         * 刷新布局
         */
        public void   reFreshMyTvText(DoctorCommnetVo doctorCommnetVo){
            if(doctorCommnetVo == null){
                return ;
            }
            Glide.with(mContext).load(doctorCommnetVo.getHimg()).placeholder(R.drawable.default_head).into(iv_img_discuss);
            tv_discuss_doc_name.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
            tv_is_satisfied.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
      /*  long date2String = DateUtil.getLongToDate(doctorCommnetVo.getPosttime(), "yyyy-MM-dd HH:mm:ss");
        String time =  DateUtil.date2String(date2String, "HH:mm:ss");*/
            String  time = DateUtil.getMyDateToSubYearAndMonthAndDay(doctorCommnetVo.getPosttime());
            tv_discuss_doc_time.setText(TextUtils.isEmpty(time) ? "" : time);
            tv_word_discuss.setText(TextUtils.isEmpty(doctorCommnetVo.getMessage()) ? "暂无评论" : doctorCommnetVo.getMessage());
            String status =  TextUtils.isEmpty(doctorCommnetVo.getScore()) ? "3" : doctorCommnetVo.getScore();
            String is_satisfied1 = "不满意";
            String is_satisfied2 = "一般";
            String is_satisfied3 = "满意";
            if(status.equals("1")){//不满意
                tv_is_satisfied.setText(is_satisfied1);
                ImageUtils.setTextViewBg(tv_is_satisfied, mContext, R.drawable.shade_full_oval_orange, is_satisfied1);
            }else if(status.equals("2")){//一般
                tv_is_satisfied.setText(is_satisfied2);
                ImageUtils.setTextViewBg(tv_is_satisfied, mContext, R.drawable.shade_full_oval_lightblue, is_satisfied2);
            }else if(status.equals("3")){//满意
                tv_is_satisfied.setText(is_satisfied3);
                ImageUtils.setTextViewBg(tv_is_satisfied, mContext, R.drawable.shade_full_oval, is_satisfied3);
            }
            String label = doctorCommnetVo.getLabel();
            mFlowLayout.removeAllViews();
            if(!TextUtils.isEmpty(label)){
                try {
                    JSONArray list = new JSONArray(label);
                    if(list !=null){
                        for (int i = 0; i <list.length() ; i++) {
                            TextView textView = new TextView(mContext);
                            ImageUtils.setTextViewBg(textView, mContext, R.drawable.shade_enpety_oval, list.getString(i));
                            ViewGroup.MarginLayoutParams marginLayoutParams =  new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 70);
                            marginLayoutParams.setMargins(15, 0, 15, 0);
                            textView.setGravity(Gravity.CENTER);
                            textView.setMinLines(40);

                            if(status.equals("1")){//不满意
                                ImageUtils.setTextViewBg(textView, mContext, R.drawable.shade_enpety_oval_orange, list.getString(i));
                                textView.setTextColor(mContext.getResources().getColor(R.color.color1));
                            }else if(status.equals("2")){//一般
                                ImageUtils.setTextViewBg(textView, mContext, R.drawable.shade_enpety_oval_blue, list.getString(i));
                                textView.setTextColor(mContext.getResources().getColor(R.color.color_blue));
                            }else if(status.equals("3")){//满意
                                ImageUtils.setTextViewBg(textView, mContext, R.drawable.shade_enpety_oval, list.getString(i));
                                textView.setTextColor(mContext.getResources().getColor(R.color.theme_color));
                            }

                            textView.setTextSize(12);
                            textView.setGravity(Gravity.CENTER);
                            mFlowLayout.addView(textView, marginLayoutParams);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
